 //
//  Fliper.m
//  yuikemall
//
//  Created by yuike6 yuike6 on 12-2-17.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "Fliper.h"
#import "header.h"


@interface Fliper(private)
-(void) showNextView;
@end

@implementation Fliper
@synthesize delegate = delegate_;
@synthesize dataSource = datasource_;
@synthesize animating;

@synthesize currentView = current_view_;
@synthesize nextView = next_view_;

@synthesize currentPage;
@synthesize numberOfPages;

@synthesize flipDirection;

@synthesize skipGestureClasses = skipGestureClasses_;

@synthesize panRecognizer = panRecognizer_;
@synthesize tapRecognizer = tapRecognizer_;

@synthesize canRotate;
@synthesize canNotFliper;

@synthesize autoFliper;

- (void) dealloc
{
    YRelease(current_view_);
    YRelease(next_view_);
    
    YRelease(skipGestureClasses_);
    
    YRelease(panRecognizer_);
    YRelease(tapRecognizer_);
    
//    YRelease(datasource_);
    
    [super dealloc];
}


-(NSInteger) numberOfPages
{
    if (numberOfPages <= 0) {
        numberOfPages = [self.dataSource numberOfPagesForPageFlipper:self];
    }
    
    return numberOfPages;
}

-(void) setNumberOfPages:(NSInteger)numberOfPages WithNilPage:(BOOL) ishas
{
    
}

- (BOOL) doSetCurrentPageEx:(NSInteger) value
{
    return TRUE;
}

- (BOOL) doSetCurrentPage:(NSInteger) value force:(BOOL) force
{
    if (self.canNotFliper) {
        return FALSE;
    }
    
    if ((value == currentPage) && 
        !force) {
		return FALSE;
	}
    
    if (self.isAnimating) {
        return FALSE;
    }
	
	flipDirection = value < currentPage ? YFlipperDirectionRight : YFlipperDirectionLeft;
	
	currentPage = value;
	
	self.nextView = [self.dataSource viewForPage:value inFlipper:self];
    
    if (!self.nextView) {
//        assert(self.nextView);
        return FALSE;
    }
    
	[self addSubview:self.nextView];
    
    if (![self doSetCurrentPageEx: value]) {
        return FALSE;
    }
	return TRUE;
}

- (BOOL) doSetCurrentPage:(NSInteger) value {
	return [self doSetCurrentPage:value force:FALSE];
}	

- (void) setCurrentPageIndexEx:(NSInteger) currentPageIndexValue
{
    // 此方法子类重写
}


- (void) setCurrentPage:(NSInteger) value force:(BOOL) force{
	if (![self doSetCurrentPage:value force:force]) {
		return;
	}
    
    [self setCurrentPageIndexEx:value];       
}

- (void) setCurrentPage:(NSInteger) value {
	[self setCurrentPage:value force:FALSE];       
} 


- (void) setCurrentPageIndexEx:(NSInteger) value animated:(BOOL) animated
{
    // 此方法子类重写
}


- (void) setCurrentPage:(NSInteger) value animated:(BOOL) animated force:(BOOL) force{
	if (![self doSetCurrentPage:value force:force]) {
		return;
	}
	[self setCurrentPageIndexEx:value animated:animated];
    
}

- (void) setCurrentPage:(NSInteger) value animated:(BOOL) animated {
	[self setCurrentPage:value animated:animated force:FALSE];
	    
}

- (void) flipeBegin     : (UIPanGestureRecognizer *) recognizer
{
//    translation = [recognizer translationInView:self].x;  
    // 子类重写此方法
}
- (void) flipeMove      : (UIPanGestureRecognizer *) recognizer
{
    
    // 子类重写此方法     
    velocity = [recognizer velocityInView:self];
  
    translation = [recognizer translationInView:self].x;	
	progress = translation / self.bounds.size.width;	
    
    
//    if(translation  < 0)
//    {
//        self.flipDirection = YFlipperDirectionLeft;
//    }
//    else
//    {
//        self.flipDirection = YFlipperDirectionRight;
//    }
    
    	if (flipDirection == YFlipperDirectionLeft) {
    		progress = MIN(progress, 0);
    	} else if (flipDirection == YFlipperDirectionRight){
    		progress = MAX(progress, 0);
    	}

}

- (void) flipeFailed:(UIPanGestureRecognizer *)recognizer
{
    // 子类重写此方法
}


- (void) flipeCanceled:(UIPanGestureRecognizer *)recognizer
{
    // 子类重写此方法
}

- (void) flipeEnd       : (UIPanGestureRecognizer *)recognizer
{
    return;
    
    // 子类重写此方法
    int nextPageIndex = self.currentPage;
    
    if (self.flipDirection == YFlipperDirectionLeft) {
        nextPageIndex ++;
    }else
    {
        nextPageIndex --;
    }
    
    self.nextView = [self.dataSource viewForPage:nextPageIndex inFlipper:self];    
    
    
    if (self.nextView) {
        self.currentPage = nextPageIndex;
    }

    [self showNextView];
}


- (void) tapGesture:(UITapGestureRecognizer *) recognizer 
{
    // 子类重写此方法
}

- (void) tapped:(UITapGestureRecognizer *) recognizer 
{
    if (animating || self.disabled) {
		return;
	}
    
    if (recognizer.state == UIGestureRecognizerStateRecognized) {
        [self tapGesture:recognizer];
    }
}

- (void) panned:(UIPanGestureRecognizer *) recognizer 
{     
        switch (recognizer.state) 
        {
            case UIGestureRecognizerStateBegan:
                self.autoFliper = NO;
                canRotate = NO;
                [self flipeBegin: recognizer];
                break;
                
              case UIGestureRecognizerStateChanged:  
//                DebugLog(@"UIGestureRecognizerStateChanged");
                canRotate = NO;
                [self flipeMove:recognizer];
                break;
                
            case UIGestureRecognizerStateFailed:
                [self flipeFailed:recognizer];
                canRotate = YES;
                break;
                
            case UIGestureRecognizerStateCancelled:
                [self flipeCanceled:recognizer];
                break;
                
            case UIGestureRecognizerStateRecognized:
                [self flipeEnd:recognizer];
                break;
                
            default:
//                DebugLog(@"default ======= %d", recognizer.state);                
                break;
        }  
    
}

- (void) init_
{
    UIPanGestureRecognizer *panRecognizer__ = [[UIPanGestureRecognizer alloc] 
                                              initWithTarget:self 
                                              action:@selector(panned:)];
    [panRecognizer__ setMaximumNumberOfTouches:1];
    panRecognizer__.delegate = self;

    self.panRecognizer = panRecognizer__;
    [self addGestureRecognizer:self.panRecognizer];
    YRelease(panRecognizer__);
    
//    UITapGestureRecognizer * _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];    
//    [_tapRecognizer setDelegate:self];
//    [_tapRecognizer requireGestureRecognizerToFail:self.panRecognizer];    
//    
//    self.tapRecognizer = _tapRecognizer;
////    [self addGestureRecognizer:self.tapRecognizer];
//    YRelease(_tapRecognizer);
    
}

- ( id )initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self init_];
    }
    
    return self;
}





//- (void) fliperPageByPageIndex:(NSInteger) pageindex
//{
//    self.nextView = [self.datasource viewForPage:pageindex inFlipper:self];    
//    
//    
//    if (self.nextView) {
//        self.currectPageIndex = pageindex;
//    }
//    
//    [self showNextView];
//
//}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch{
    
    for (Class aClass in skipGestureClasses_) {
        if([touch.view isKindOfClass:[aClass class]])
        {
            return NO;
        }
    }    
    
    return YES;  
}

- (void) registeSkipGestureClass:(Class) aClass
{
    if (!skipGestureClasses_) {
        skipGestureClasses_ = [[NSMutableArray alloc] init];
    }

    [skipGestureClasses_ addObject:aClass];    
}

-(void) reAdjustLayout
{
    [self.currentView setFrame:self.bounds];
}


-(void) refresh
{
    
}


- (BOOL) flipFirstPage
{
    return TRUE;
}

- (BOOL) flipLastPage
{
    return TRUE;
}

@end
